﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;
using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsPageCopyDataCommand : CmsPageCopyCommand
    {
        public override CmsPage ExecuteOne()
        {
            CmsPage result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsPage>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsPage>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsPage, int>>();

                var data = SqlUtility<CmsPage, int>.ExecuteReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_PageCopy",
                        new
                        {
                            CommandParameter.PageId,
                            CommandParameter.CreatedBy,
                            CommandParameter.DateCreated,
                            PageTypeParameter = "Page", //TO DO - remove hardcoded value
                            ContentTypeParameter = "Content",
                            PageContentTypeParameter = CmsContentType.PageContentSystemContentType,
                            PageControlTypeParameter = "PageControl"
                        },
                        mapper);

                result = data != null ?
                    data.FirstOrDefault() :
                    null;
            }

            return result;
        }
    }

}
